//
//  NetworkTools.m
//  DuoDian
//
//  Created by 张卓 on 2017/8/16.
//  Copyright © 2017年 bigcastcloudShop. All rights reserved.
//

#import "NetworkTools.h"

@implementation NetworkTools
// 全局访问点
+(instancetype)shared{
    static dispatch_once_t onceToken;
    static NetworkTools *instance;
    dispatch_once(&onceToken, ^{
        instance = [NetworkTools new];
        instance.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"text/html",@"text/javascript",@"text/xml", nil];
    });
    return instance;
}
// AFN初步封装
- (void)requestMethod:(BCNetworkToolsMethod)method urlString:(NSString *)urlString parameters:(id)parameters success: (void(^)(id response))success failure:(void(^)(NSError * error)) failure{
    //    version  应用版本
    //    os 系统类型 iso android
    //    os_version    系统版本
    NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    NSString * os_version= [NSString stringWithFormat:@"%f" ,[[[UIDevice currentDevice] systemVersion] floatValue]] ;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:parameters];
    [dic setObject:version forKey:@"version"];
    [dic setObject:@"ios" forKey:@"os"];
    [dic setObject:os_version forKey:@"os_version"];
    id parametersDic = dic.copy;
    // get 请求
    if (method == GET) {
        [self GET:urlString parameters:parametersDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            success(responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            failure(error);
        }];
    }else {
        // post 请求
        [self POST:urlString parameters:parametersDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            success(responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            failure(error);
        }];
    }
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusUnknown:
                NSLog(@"未知网络");
                break;
            case AFNetworkReachabilityStatusNotReachable:
                NSLog(@"无网络");
                [[NSNotificationCenter defaultCenter] postNotificationName:@"NotReachable" object:@"NotReachable"];
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                NSLog(@"WiFi网络");
                [[NSNotificationCenter defaultCenter] postNotificationName:@"NotReachable" object:@"WiFi"];
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
                NSLog(@"移动网络");
                [[NSNotificationCenter defaultCenter] postNotificationName:@"NotReachable" object:@"WWAN"];
                break;
            default:
                break;
        }
    }];
    [manager startMonitoring];
}
@end
